package com.rc;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.util.AuthUtil;
import com.util.RedisClient;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

public class MyGatewayFilter extends ZuulFilter {


    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
    }

    @Override
    public boolean shouldFilter() {
        final String serviceId = (String) RequestContext.getCurrentContext().get("proxy");
        return "microClient".equals(serviceId);
    }


    @Override
    public Object run() {


        RequestContext ctx = RequestContext.getCurrentContext();

        HttpServletRequest request = ctx.getRequest();
        System.out.println(request.getMethod()+request.getServletPath());
        System.out.println(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

       Integer  authorization = 500;




        ShardedJedis jedis = null;
        try {
            jedis = RedisClient.getRedisClient();
            String str = jedis.get("microClient");
            List<Map>  jsonObject = JSONObject.parseArray(str, Map.class);

            authorization =  AuthUtil.authVerifyReqUrl(jsonObject , request.getMethod()+request.getServletPath());


        } catch (JedisConnectionException e) {
            // LOGGER.error("redis 链接异常: " + e.getMessage());
            //  counter();
        } catch (Exception e) {
            //  LOGGER.error("redis 异常: " + e.getMessage(), e);
        } finally {
            jedis.close();
        }
        switch (authorization) {
            case 404:
                ctx.setSendZuulResponse(false);//过滤该请求, 不对其进行路由
                ctx.setResponseStatusCode(404);//返回错误码
                ctx.setResponseBody("Request is 404!");//返回错误内容
                return null;//Zuul还未对返回数据做处理
            case 0:
                return null;
            case 1:
                System.out.println("####################"+authorization);
                Object accessToken = request.getHeader("token");//获取token参数
                ctx.addZuulRequestHeader("CURRENT_USER","1");
                if (accessToken == null) {
                    System.out.println();
                    System.out.println("token is empty");
                    ctx.setSendZuulResponse(false);//过滤该请求, 不对其进行路由
                    ctx.setResponseStatusCode(401);//返回错误码
                    ctx.setResponseBody("token is null!");//返回错误内容
                    return null;//Zuul还未对返回数据做处理
                }else {
                    return null;
                }

            case 2:
                System.out.println("####################"+authorization);
                break;
            case 500:
                ctx.setSendZuulResponse(false);//过滤该请求, 不对其进行路由
                ctx.setResponseStatusCode(500);//返回错误码
                ctx.setResponseBody("token is null!");//返回错误内容
                return null;//Zuul还未对返回数据做处理
        }



        return null;
    }



}